import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.rowset.*;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MaximizeAction;

import com.sun.rowset.*;

public class DataProcessImplement extends UnicastRemoteObject implements DataProcess{
	
	private Connection con;
	private Statement stmt;

	protected DataProcessImplement() throws RemoteException {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=QLSV","sa","khang123");
			stmt = con.createStatement();
			} catch (Exception e) {
			e.printStackTrace();
			}
	}

	@Override
	public WebRowSet GetTable(String tableName) throws RemoteException {
		WebRowSet rs = null;
		try {
		String sql = "select * from " + tableName;
		ResultSet r = stmt.executeQuery(sql);
		rs=new WebRowSetImpl();
		rs.populate(r);
		} catch (Exception e) {
		e.printStackTrace();
		}
		return rs;
	}

	@Override
	public int ExecUpdateSQL(String sql) throws RemoteException {

		int value=Integer.MAX_VALUE;
		try {
			value=stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return value;
	}

	@Override
	public WebRowSet GetRecord(String condition, String tableName)
			throws RemoteException {WebRowSet rs = null;
			try {
				String sql = "select * from " + tableName + " where " + condition;
				ResultSet r = stmt.executeQuery(sql);
				rs=new WebRowSetImpl();
				rs.populate(r);
				} catch (Exception e) {
				e.printStackTrace();
				}
				return rs;
	}

	@Override
	public ResultSet getTable(String tableName) throws RemoteException {
		ResultSet rst=null;
		String sql="Select * from " + tableName;
		try {
			rst=stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rst;
	}

	@Override
	public WebRowSet GetDataBySQL(String sql) throws RemoteException{
		WebRowSet rs = null;
		try {
			ResultSet r = stmt.executeQuery(sql);
			rs=new WebRowSetImpl();
			rs.populate(r);
			} catch (Exception e) {
			e.printStackTrace();
			}
			return rs;
	}

}
